/* 
 * File:   PathValidator.cpp
 * Author: Guilherme Monteiro
 * 
 */

#include "../headers/PathValidator.h"

PathValidator::PathValidator() {
}

PathValidator::PathValidator(const PathValidator& orig) {
}

PathValidator::~PathValidator() {
}

/**
 * Validate the path depending of your type: 
 * to a low resolution image or to a high resolution image.
 * 
 * The low image path must have the follow format, for pages (example):
 * "/books/caracterizacao/baixa_resolucao/image278.png"
 * 
 * The low image path must have the follow format, for tables (example):
 * "/books/caracterizacao/baixa_resolucao/tabelasAlta/image278.png"
 * 
 * The high image path must have the follow format, for pages (example):
 * "/books/caracterizacao/alta_resolucao/image278.png"
 * 
 * The high image path must have the follow format, for tables (example):
 * "/books/caracterizacao/alta_resolucao/tabelasBaixa/image278.png"
 * 
 * @param type: type of image in path
 * @param path: path to low or high page image
 * @return true if the path is valid
 */
bool PathValidator::validatePathToPages(TYPE_PATH type, string path) {
    if(&path == 0 || path.empty()) {
        return false;
    }
    
    if(count(path.begin(), path.end(), '/') != 4) {
        return false;
    }

    if(!verifyBooks(path)) {
        return false;
    } 
    
    if(type == LOW_PAGE) {
        string::size_type posLowResolution = path.find("baixa_resolucao");
        if(posLowResolution == string::npos) {
            return false;
        }
    }
    else if(type == HIGH_PAGE) {
        string::size_type posHighResolution = path.find("alta_resolucao");
        if(posHighResolution == string::npos) {
            return false;
        }
    }

    return true;
}

/**
 * Validate the path depending of your type: 
 * to a low resolution image or to a high resolution image.
 * 
 * The low image path must have the follow format, for tables (example):
 * "/books/caracterizacao/metadados/tabelasAlta/image278.png"
 * 
 * The high image path must have the follow format, for tables (example):
 * "/books/caracterizacao/metadados/tabelasBaixa/image278.png"
 * 
 * @param type: type of image in path
 * @param path: path to low or high image table
 * @return true if the path is valid
 */
bool PathValidator::validatePathToTables(TYPE_PATH type, string path) {
    if(&path == 0 || path.empty()) {
        return false;
    }
    
    if(count(path.begin(), path.end(), '/') != 5) {
        return false;
    }

    if(!verifyBooks(path)) {
        return false;
    }
    
    if(type == LOW_TABLE) {
        string::size_type posLowResolution = path.find("tabelasBaixa");
        if(posLowResolution == string::npos) {
            return false;
        }
    }
    else if(type == HIGH_TABLE) {
        string::size_type posHighResolution = path.find("tabelasAlta");
        if(posHighResolution == string::npos) {
            return false;
        }
    }
    
    string::size_type posMeta = path.find("metadados");
    if(posMeta == string::npos) {
        return false;
    }

    return true;
}

/**
 * Verify if books is contained in path
 * 
 * @param path
 * @return true if is contained 
 */
bool PathValidator::verifyBooks(string path) {
    string::size_type posBooks = path.find("books");
    if(posBooks == string::npos) {
        return false;
    }
    return true;
}

/**
 * Returns if the path contains ".."
 * 
 * @param path: path analyzed
 * @return true if contains
 */
bool PathValidator::validateDotDot(string path) {
    string::size_type posBooks = path.find("..");
    if(posBooks == string::npos) {
        return false;
    }
    return true;
}
